package net.huashitong.supervision.insideDuCha.util;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.util.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class CommUtil {


    public static String[] changeTimeType(int timeType, String period, String day, String week, String month, String quarter, String timelimit, String timelimits){

        String  timeclas= "0";
        String cycleValue =null;
        String [] timePeriod ={"0","0"};
        switch (timeType){
            case 1: //单时限

            timeclas =1+"";
            cycleValue = timelimit;

                break;
            case 2: //周期性
                switch (period){
                    case "0": //天
                        timeclas="2";
                        cycleValue=day;
                        break;
                    case "1": //周
                        timeclas="3";
                        cycleValue=week;
                        break;
                    case "2":  //月
                        timeclas="4";
                        cycleValue=month;
                        break;
                    case "3":
                        //季度
                        timeclas="5";
                        cycleValue=quarter;
                        break;
                }
                break;
            case 3: // 多时限
                timeclas="6";
                cycleValue= timelimits;

                break;
            default:
                break;
        }
        timePeriod[0]=timeclas;
        timePeriod[1]=cycleValue;

        return timePeriod;
    }


    public static String remindDate(int timeType, String cycleValue){
       String remindDate="";
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
       // 1 单时限 2 天，3 周，4 月，5季度  6 多时限
        switch (timeType)
        {
            case 1:
                remindDate=cycleValue;
                break;
            case 2:
                int addDay = Integer.parseInt(cycleValue);
                ca.add(Calendar.DATE,addDay);
                remindDate = DateUtil.DateToStr(ca.getTime());
                break;
            case 3:
                int weeks = ca.get(Calendar.DAY_OF_WEEK);
                int wk =Integer.parseInt(cycleValue);
                wk=wk+1;
                int abs = Math.abs(weeks - wk);
                ca.add(Calendar.DATE,abs);
                remindDate = DateUtil.DateToStr(ca.getTime());
                break;
            case 4:
               // cycleValue=month;
                int currDay = ca.get(Calendar.DAY_OF_MONTH);
                int temDay =Integer.parseInt(cycleValue);

                String currentMonth = DateUtil.getCurrentMonth();

                if (currDay-temDay>0) {
                    //提醒日期在  早于 今天，提醒日期设置为下个月的
                    ca.add(Calendar.MONTH,1);
                    ca.getTime();
                    int maximum = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
                    int year = ca.get(Calendar.YEAR);
                    int month2 =ca.get(Calendar.MONTH)+1;
                    if(maximum-temDay<=0){//考虑2月情况
                        remindDate=year+"-"+month2+"-"+maximum;
                    }else {
                        remindDate=year+"-"+month2+"-"+temDay;
                    }
                }else {
                    remindDate= currentMonth+"-"+temDay;
                }
                break;
            case 5:
                Date alertDate = DateUtil.strToDate(cycleValue);
                Date currDate = DateUtil.strToDate(DateUtil.getCurrentDate());
                if (alertDate.compareTo(currDate)>0) {
                    //提醒日期 是否晚于 今天 如果晚于的话，直接设置。
                    remindDate = cycleValue;
                }else {
                    //下一个季度
                    ca.setTime(alertDate);
                    ca.add(Calendar.MONTH,3);
                    remindDate = DateUtil.DateToStr(ca.getTime());
                }
                break;
            case 6:
                String[] split = cycleValue.split(",");
                remindDate=split[0];
                break;
            default:
                break;

        }
        remindDate= DateUtil.dateFormat(remindDate,"yyyy-MM-dd");

        return remindDate;
    }
}
